package io.renren.modules.school.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.school.entity.PrefessionLowScoreEntity;
import io.renren.modules.school.entity.ProfessionEntity;
import io.renren.modules.school.entity.SchoolEntity;
import io.renren.modules.school.service.ProfessionLowScoreService;
import io.renren.modules.school.service.ProfessionService;
import io.renren.modules.school.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 
 *
 * @author xuebing
 * @email xuebinggod@163.com
 * @date 2018-10-11 19:14:21
 */
@RestController
@RequestMapping("school/profession")
public class ProfessionController {
    @Autowired
    private ProfessionService professionService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private ProfessionLowScoreService professionLowScoreService;
    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("school:profession:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = professionService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/getProfessions")
    // @RequiresPermissions("school:school:list")
    public R getProfessions(@RequestBody Map<String, Object> params){
        List l=professionService.getProfessions(params);
        return R.ok().put("professions",l);
    }

    /**
     * 列表
     */
    @RequestMapping("/getProfessionLowestScore")
    // @RequiresPermissions("school:profession:list")
    public R getProfessionLowestScore(@RequestParam Map<String, Object> params){
        PageUtils page = professionLowScoreService.queryPage(params);
        return R.ok().put("page", page);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
   // @RequiresPermissions("school:profession:info")
    public R info(@PathVariable("id") Integer id){
        ProfessionEntity profession = professionService.selectById(id);

        return R.ok().put("profession", profession);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("school:profession:save")
    public R save(@RequestBody ProfessionEntity profession){
        SchoolEntity s=schoolService.selectById(profession.getSchoolId());
        if(s!=null){
            profession.setSchoolCode(s.getCode());
            profession.setSchoolName(s.getName());
        }
        professionService.insert(profession);

        return R.ok();
    }

    @RequestMapping("/saveMall")
    @ResponseBody
    public R saveMall(@RequestParam(value="file",required = false) MultipartFile file, HttpServletRequest request) {

        professionService.saveMall(file);

        return R.ok("上传成功",100);
    }

    /**
     * 保存
     */
    @RequestMapping("/saveProfessionYear")
    //@RequiresPermissions("school:profession:save")
    public R saveProfessionYear(@RequestBody PrefessionLowScoreEntity profession){
        SchoolEntity s=schoolService.selectById(profession.getSchoolId());
        if(s!=null){
            profession.setSchoolCode(s.getCode());
            profession.setSchoolName(s.getName());
        }
        PrefessionLowScoreEntity p=professionService.getOnePLS(profession);
        if(p!=null){
            return R.ok("您已插入过了",101);
        }else {
            professionService.insertProfessionYear(profession);
            return R.ok();
        }
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("school:profession:update")
    public R update(@RequestBody ProfessionEntity profession){
        ValidatorUtils.validateEntity(profession);
        professionService.updateAllColumnById(profession);//全部更新
        
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/updateLowScore")
    //@RequiresPermissions("school:profession:update")
    public R updateLowScore( ProfessionEntity profession){
        ValidatorUtils.validateEntity(profession);
        professionService.updateById(profession);//全部更新

        return R.ok();
    }



    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("school:profession:delete")
    public R delete( Integer id){
        professionService.deleteBatchIds(Arrays.asList(id));

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteYearScore")
    // @RequiresPermissions("school:profession:delete")
    public R deleteYearScore( Integer id){
        professionLowScoreService.deleteBatchIds(Arrays.asList(id));
        return R.ok();
    }

}
